package org.kzwl.service.web.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.kzwl.comm.pojo.ResponseData;
import org.kzwl.comm.pojo.bo.QueryAdminWebCommentsBO;
import org.kzwl.comm.pojo.bo.QueryWebCommentsBO;
import org.kzwl.comm.pojo.bo.SaveCuWebCommentsBO;
import org.kzwl.comm.pojo.bo.UpdateWebCommentsBO;
import org.kzwl.entity.web.CuWebComments;
import org.kzwl.mapper.web.CuWebCommentsMapper;
import org.kzwl.service.app.UserService;
import org.kzwl.service.web.CuWebCommentsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

@Service
public class CuWebCommentsServiceImpl extends ServiceImpl<CuWebCommentsMapper, CuWebComments> implements CuWebCommentsService {

    @Resource
    UserService userService;
    @Override
    public ResponseData<Void> saveComments(SaveCuWebCommentsBO saveCuWebCommentsBO) {
        CuWebComments cuWebComments = new CuWebComments();
        BeanUtils.copyProperties(saveCuWebCommentsBO,cuWebComments);
        this.baseMapper.insert(cuWebComments);
        return new ResponseData<>();
    }

    @Override
    public ResponseData<IPage<CuWebComments>> queryComments(QueryWebCommentsBO queryWebCommentsBO) {
        Page<CuWebComments> cuWebCommentsPage = this.baseMapper.selectPage(new Page<>(queryWebCommentsBO.getCurrent(), queryWebCommentsBO.getSize()), new LambdaQueryWrapper<CuWebComments>()
                .eq(CuWebComments::getCuExt1, queryWebCommentsBO.getCuExt1()).eq(CuWebComments::getCuCommentsIs,1));

        return new ResponseData<>(this.setPages(cuWebCommentsPage));
    }


    public Page<CuWebComments> setPages(Page<CuWebComments> page){
        if(page.getRecords().size() >0)
        {
            page.getRecords().forEach(item ->{
                item.setCuExt2(userService.list().stream().filter(i->item.getCuUserId() == i.getCuUserId()).collect(Collectors.toList()).get(0).getCuUsername());
            });
        }
        return page;
    }

    @Override
    public ResponseData<IPage<CuWebComments>> queryAdminComments(QueryAdminWebCommentsBO queryAdminWebCommentsBO) {
        Page<CuWebComments> cuWebCommentsPage = this.baseMapper.selectPage(new Page<>(queryAdminWebCommentsBO.getCurrent(), queryAdminWebCommentsBO.getSize()), new LambdaQueryWrapper<CuWebComments>().eq(queryAdminWebCommentsBO.getCuCommentsIs() != 3, CuWebComments::getCuCommentsIs, queryAdminWebCommentsBO.getCuCommentsIs()));

        return new ResponseData<>(this.setPages(cuWebCommentsPage));
    }

    @Override
    public ResponseData<Void> updateAdminComments(UpdateWebCommentsBO updateWebCommentsBO) {
        CuWebComments cuWebComments = new CuWebComments();
        BeanUtils.copyProperties(updateWebCommentsBO,cuWebComments);
        this.updateById(cuWebComments);
        return new ResponseData<>();
    }
}
